package com.cherrish.demo.thread;

/**
 * @author oxchains
 * @time 2018-10-30 16:29
 * @name VolatitleTest
 * @desc:
 */
public class VolatitleTest {
    private static volatile int n1 = 0;
    private static int n2 = 0;
    private static int n3 = 0;

    public static void main(String[] args) {
        new VolatitleTest().test2();
    }

    public void test1(){
        new Thread(){
            @Override
            public void run() {
                try{
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                n1 = 1;
            }
        }.start();
        while (n1 == 0){

        }
        System.out.println("test1 end");
    }
    public void test2(){
        new Thread(){
            @Override
            public void run() {
                try{
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                n2 = 2;
            }
        }.start();
        while (n2 == 0){
            System.out.println(n2);
        }
        System.out.println("test2 end");
    }

    public void test3(){
        new Thread(){
            @Override
            public void run() {
                try{
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                n3 = 3;
            }
        }.start();
        while (n3 == 0){
        }
        System.out.println("test3 end");
    }
}
